<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>绑定手机号</title>
  <link rel="stylesheet" type="text/css" href="/static/study/css/base2.css">
  <style>
    #getVerifyCode{
      width: 160px;
      float:left;
      height: 45px;
      background-color: #ff9900;
      color: #ffffff;
      border: none;
      border-radius: 5px;
      margin-top: 20px;
    }
    #getVerifyCode:disabled {
      background-color: #ccc;
    }
    form > p{
      width: 100%;
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
    }
    #phone-msg{
      font-size: 12px;
      color: red;
    }
    .other-login{
      width: 90%;
      margin: 0 auto;
      margin-top: 20px;
      height: 30px;
      display: flex;
      justify-content: space-around;
      align-items: center;
    }
    .other-line{
      width: 96px;
      height: 1px;
      background-color: #AEAEAE;
    }
    .other-text{
      color: #AEAEAE;
    }
  </style>
</head>
<body>
  <div id="main">
    <h2>
      能力数据
    </h2>
    <div id="con">
      <div id="login-pwd">
        <h3 style="color: #666666;" >绑定手机号，完成注册</h3>
        <form method="post" action={% url 'study:bindPhone' %}>
          {% csrf_token %}
          <input type="hidden" name="prev_url" value={{prev_url}} />
          <p>
            <input type="text" value="" required placeholder="请输入手机号" id="phone" class="phone" name="phone" />
          </p>
          <p>
            <span id="phone-msg"></span>
          </p>
          <p>
            <input type="text" required placeholder="请输入短信验证码" class="password" name="verifyCode" />
            <input type="button" id="getVerifyCode" value="获取短信验证码" />
          </p>
          {% if error %}
          <p class="error-msg">
            {{error}}
          </p>
          {% endif %}
          <input id="submit" type="submit" value="绑定">
        </form>
      </div>
    </div>
  </div>
  <script src="/static/js/js-cookie.js"></script>
  <script src="/static/study/layui/layui.js" charset="utf-8"></script>
  <script src="/static/js/tools.js"></script>
  <script>
    var interval;
    var secs = 60;
    var counting = false;
    var layer
    verifyBtn = document.getElementById('getVerifyCode')
    layui.use('layer', function(){
      layer = layui.layer;
    });  
    verifyBtn.addEventListener('click', function() {
      if(counting) return;
      var phoneDom = document.getElementById('phone')
      if(!(/^1\d{10}$/.test(phoneDom.value))){ 
        showErr("phone", "请输入正确手机号");  
        return false; 
      } 
      counting= true;
      _post("{% url 'study:getVerifyCode' %}", {phone: phoneDom.value}, function(res){
        interval = setInterval(function() {
          verifyBtn.value = secs-- + 's后重新获取';
          verifyBtn.disabled = true
          if(secs<=0) {
            clearInterval(interval);
            verifyBtn.value = '获取短信验证码';
            secs = 60;
            counting = false;
            verifyBtn.disabled = false
          }
        }, 1000)
      }, function(res){
        counting = false;
        layer.msg(res.message, {
          time: 2000 //2秒关闭（如果不配置，默认是3秒）
        });   
      })
    })
  </script>
</body>
</html>